{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Real Agent 使用示例\n",
    "\n",
    "本 notebook 演示如何使用 Real Agent 框架进行智能体之间的交互"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\u001b[32m2025-01-29 09:16:09.894\u001b[0m | \u001b[1mINFO    \u001b[0m | \u001b[36mbyzerllm.utils.connect_ray\u001b[0m:\u001b[36mconnect_cluster\u001b[0m:\u001b[36m48\u001b[0m - \u001b[1mJDK 21 will be used (/Users/allwefantasy/.auto-coder/jdk-21.0.2.jdk/Contents/Home)...\u001b[0m\n",
      "2025-01-29 09:16:09,938\tINFO worker.py:1564 -- Connecting to existing Ray cluster at address: 127.0.0.1:6379...\n",
      "2025-01-29 09:16:09,958\tINFO worker.py:1740 -- Connected to Ray cluster. View the dashboard at \u001b[1m\u001b[32m127.0.0.1:8265 \u001b[39m\u001b[22m\n"
     ]
    }
   ],
   "source": [
    "# 初始化环境\n",
    "import byzerllm\n",
    "from real_agent.agent import Agent, Group\n",
    "\n",
    "# 创建 LLM 实例\n",
    "llm = byzerllm.ByzerLLM.from_default_model(\"deepseek_chat\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "智能体创建完成\n"
     ]
    }
   ],
   "source": [
    "# 创建三个智能体\n",
    "alice = Agent(\"Alice\", llm)\n",
    "bob = Agent(\"Bob\", llm)\n",
    "charlie = Agent(\"Charlie\", llm)\n",
    "\n",
    "print(\"智能体创建完成\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<real_agent.agent.GroupMembership at 0x13f21baf0>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建开发群组\n",
    "dev_group = Group(\"dev\")\n",
    "alice.join_group(dev_group)\n",
    "# Bob 和 Charlie 加入群组\n",
    "bob.join_group(dev_group)\n",
    "charlie.join_group(dev_group)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 群组定向消息\n",
    "dev_group.talk_to_all(\"大家好，我是新人Alice！\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 群组定向消息\n",
    "dev_group.talk_to(\"这个问题谁能回答？\", mentions=[bob])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Private Chat Start] Alice -> Charlie: 能私下请教个问题吗？\n",
      "[Private Chat Start] Alice -> Charlie: 当然可以，请问有什么问题？\n",
      "[Private Chat Start] Alice -> Charlie: 请具体说明您的问题，我会尽力帮助您。\n",
      "[Private Chat Start] Alice -> Charlie: 请具体说明您的问题，我会尽力帮助您。\n",
      "[Private Chat End] Charlie ignored the message\n"
     ]
    }
   ],
   "source": [
    "# 私聊示例\n",
    "alice.talk_to(charlie, \"能私下请教个问题吗？\",print_conversation=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 查看 Alice 的私聊记录\n",
    "print(\"Alice 与 Charlie 的私聊记录：\")\n",
    "for msg in alice.private_chats.get(\"Charlie\", []):\n",
    "    print(f\"{msg.sender}: {msg.content}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 查看 dev 群组的历史记录\n",
    "group = alice.joined_groups[\"dev\"]\n",
    "print(\"dev 群组历史记录：\")\n",
    "for msg in group.history:\n",
    "    print(f\"{msg.sender}: {msg.content}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
